<?php

namespace App\Api\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class SellPackageBatchModel extends Model
{
    use HasFactory;

    protected $table = 'sell_package_batch';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function storageBatchInfo()
    {
        return SELF::hasOne(StorageBatchModel::class, 'id', 'storage_batch_id');
    }

    static public function getTargetBatchInfo($id)
    {
        return SELF::where('id', $id)->first();
    }

    static public function getAllBatchNum($packageContentId)
    {
        return SELF::select(
            DB::raw('SUM(rest_num) as total_rest_num')
        )->where('sell_package_content_id', $packageContentId)->first();
    }
}
